<template>
  <div class="printForm">
    <div class="area-header">
      <span class="area-header-title">{{ $t('申请单') }}</span>
    </div>
    <table class="printTable">
      <tr>
        <td class="tit">{{ $t('上市项目') }}</td>
        <td>{{ form.dealName }}</td>
        <td class="tit">{{ $t('证券代码') }}</td>
        <td>{{ form.stockCode }}</td>
      </tr>
      <tr>
        <td class="tit">{{ $t('我方持有总股数(万股)') }}</td>
        <td>
          {{ moneyFormat(form.shareHold) }}
        </td>
        <td class="tit">{{ $t('持股比例(%)') }}</td>
        <td>{{ moneyFormat(form.shareRate) }}</td>
      </tr>
      <tr>
        <td class="tit">{{ $t('公告') }}</td>
        <td>
          {{ form.noticeTitle }}
        </td>
        <td class="tit">{{ $t('减持股数不超过(万股)') }}</td>
        <td>{{ moneyFormat(form.shareNotExceeding) }}</td>
      </tr>
      <tr>
        <td class="tit">{{ $t('减持比例不超过(%)') }}</td>
        <td>
          {{ moneyFormat(form.rateNotExceeding) }}
        </td>
        <td class="tit">{{ $t('预计公告发布日期') }}</td>
        <td>{{ parseTime(form.releaseDate) }}</td>
      </tr>
      <tr>
        <td class="tit">{{ $t('预计减持日期') }}</td>
        <td colspan="3">{{ form.reducingDate }}</td>
      </tr>
    </table>
    <div v-if="form.detailList && form.detailList.length > 0" style="margin-bottom: 15px;">
      <div class="area-header">
        <span class="area-header-title">{{ $t('实际减持明细维护') }}</span>
      </div>
      <table class="previewTable" style="table-layout: fixed;">
        <thead>
          <th>{{ $t('证券账号') }}</th>
          <th>{{ $t('实际减持总金额') + $t('金额单位') }}</th>
          <th>{{ $t('实际减持总股数(万股)') }}</th>
          <th>{{ $t('减持均价(元/股)') }}</th>
          <th>{{ $t('减持税费') + $t('金额单位') }}</th>
          <th>{{ $t('减持费率(%)') }}</th>
          <th>{{ $t('交易方式') }}</th>
          <th>{{ $t('减持日期') }}</th>
          <th>{{ $t('交易对手') }}</th>
        </thead>
        <tbody>
          <tr v-for="(v, ind) in form.detailList" :key="ind">
            <td>{{ v.securityAccount }}-{{ v.securityName }}</td>
            <td>{{ moneyFormat(v.totalSubAmount, 'w') }}</td>
            <td>{{ moneyFormat(v.totalSubShare) }}</td>
            <td>{{ moneyFormat(v.subPrice) }}</td>
            <td>{{ moneyFormat(v.subTaxe, 'w') }}</td>
            <td>{{ moneyFormat(v.subFeeRate) }}</td>
            <td>{{ wayFormat(v.tradingWay) }}</td>
            <td>{{ parseTime(v.subDate) }}</td>
            <td>{{ v.opponentName }}</td>
          </tr>
        </tbody>
      </table>
    </div>
    <div v-if="historyDataList && historyDataList.length > 0" style="margin-bottom: 15px;">
      <div class="area-header">
        <span class="area-header-title">{{ $t('股票历史减持信息') }}</span>
      </div>
      <table class="previewTable" style="table-layout: fixed;">
        <thead>
          <th>{{ $t('上市项目') }}</th>
          <th>{{ $t('证券代码') }}</th>
          <th>{{ $t('证券账号') }}</th>
          <th>{{ $t('实际减持总金额') + $t('金额单位') }}</th>
          <th>{{ $t('实际减持总股数(万股)') }}</th>
          <th>{{ $t('减持均价(元/股)') }}</th>
          <th>{{ $t('减持比例(%)') }}</th>
          <th>{{ $t('交易方式') }}</th>
          <th>{{ $t('减持日期') }}</th>
        </thead>
        <tbody>
          <tr v-for="(v, ind) in historyDataList" :key="ind">
            <td>{{ v.dealName }}</td>
            <td>{{ v.stockCode }}</td>
            <td>{{ v.securityAccount }}-{{ v.securityName }}</td>
            <td>{{ moneyFormat(v.totalSubAmount, 'w') }}</td>
            <td>{{ moneyFormat(v.totalSubShare) }}</td>
            <td>{{ moneyFormat(v.subPrice) }}</td>
            <td>{{ moneyFormat(v.subShareRate) }}</td>
            <td>{{ wayFormat(v.tradingWay) }}</td>
            <td>{{ parseTime(v.subDate) }}</td>
          </tr>
        </tbody>
      </table>
    </div>

  </div>
</template>

<script>
import { listHistory } from '@/api/deal/listed/shareSub/listedShareSubDetail'
import { getListedShareSub } from '@/api/deal/listed/shareSub/listedShareSub'
import { getListedReducingNotice } from '@/api/deal/listed/reducing/listedReducingNotice'
import { getListedDeal } from '@/api/deal/listed/base/listedDeal'
import { listListedPositionHold } from '@/api/deal/listed/position/listedPositionHold'
export default {
  components: {
  },
  data() {
    return {
      form: {
        fileList: []
      },
      noticeShow: false,
      tradingWayOptions: [],
      historyDataList: [],
      queryParams: {
        listedId: '',
        shareId: '',
        pageNum: 1,
        pageSize: 10
      }
    }
  },
  mounted() {
    this.$bus.$emit('async_done')
    // 交易方式字典
    this.getDicts('listed_trading_way').then(response => {
      this.tradingWayOptions = response.data
    })
  },
  methods: {
    // 初始化数据
    init(data) {
      if (data != null && data.id !== undefined && data.id !== '' && data.id !== null) {
        getListedShareSub(data.id).then(response => {
          this.form = response.data
          this.selectListDeal(this.form.listedId)
          this.selectNotice()
        })
      }
    },
    // 选择上市项目事件，获取选择的项目的其他关联数据
    selectListDeal(id) {
      // 获取关联数据
      getListedDeal(id).then(res => {
        this.form.stockCode = res.data.stockCode
        this.form.shareHold = res.data.shareHold
        this.form.shareRate = res.data.shareRate
        this.form.dealId = res.data.dealId
        this.form.dealName = res.data.dealName
        if (res.data.shareRate > 5) {
          this.noticeShow = true
        }
        // 获取股票历史减持信息
        this.queryParams.listedId = id
        this.getHistoryList()
        if (this.form.id === undefined || this.form.id === '') {
          this.form.detailList = []
          // 获取当前上市项目的持仓证券股数
          this.getSecurityByListedId()
        }
      })
    },
    // 获取股票历史减持信息
    getHistoryList() {
      this.queryParams.shareId = this.form.id
      listHistory(this.queryParams).then(response => {
        this.historyDataList = response.rows
        this.historyTotal = response.total
      })
    },
    // 根据上市项目ID，获取持仓信息
    getSecurityByListedId() {
      this.queryParams.listedId = this.form.listedId
      listListedPositionHold(this.queryParams).then(res => {
        res.rows.forEach(item => {
          // 添加dataList 股票减持详情
          const row = {
            securityId: item.securityId,
            totalSubAmount: '',
            totalSubShare: '',
            subPrice: '',
            subTaxe: '',
            subFeeRate: '',
            tradingWay: '',
            subDate: new Date(),
            opponentId: ''
          }
          if (!(this.form.detailList instanceof Array)) {
            this.$set(this.form, 'detailList', [])
          }
          this.$nextTick(() => {
            this.form.detailList.push(row)
          })
        })
      })
    },
    // 选择减持公告事件，获取选择的公告的其他关联数据
    selectNotice() {
      // 获取关联数据
      getListedReducingNotice(this.form.noticeId).then(res => {
        if (res.data) {
          this.form.shareNotExceeding = res.data.shareNotExceeding
          this.form.rateNotExceeding = res.data.rateNotExceeding
          this.form.releaseDate = res.data.releaseDate
          this.form.reducingDate = this.parseTime(res.data.reducingDateStart) + '  ~  ' + this.parseTime(res.data.reducingDateEnd)
        }
      })
    },
    wayFormat(row) {
      return this.selectDictLabel(this.tradingWayOptions, row)
    }

  }

}
</script>
